"""全局函数配置

包含Python全局函数的C++实现配置
"""

# ============================================================================
# 类型变量定义
# ============================================================================
# TYPE_VARS 定义函数模板参数，通过 bound 或 constraints 指定约束
#
# 约束方式：
# 1. bound: 引用 Protocol（推荐） - "bound": "Comparable"
# 2. constraints: 具体类型列表 - "constraints": ["int", "float"]
#
# 扩展示例：
# stubs/mc/types.py 中定义 int32_t 并实现 Integer 协议的方法，
# 则 int32_t 可以自动用于所有 bound="Integer" 的函数
TYPE_VARS = {
    # 基础泛型参数（无约束）
    "T": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T1": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T2": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T3": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T4": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T5": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T6": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T7": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T8": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T9": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "T10": {
        "description": "通用泛型参数",
        "pass_by": "const_ref",
    },
    "K": {
        "description": "键类型",
        "pass_by": "const_ref",
    },
    "V": {
        "description": "值类型",
        "pass_by": "const_ref",
    },
    # 数值类型约束（引用 Protocol）
    "T_numeric": {
        "description": "数值类型（int, float 或任何实现 Number 协议的类型）",
        "bound": "Number",
        "pass_by": "value",
    },
    "T_int": {
        "description": "整数类型（int 或任何实现 Integer 协议的类型，如 int32_t）",
        "bound": "Integer",
        "pass_by": "value",
    },
    "T_float": {
        "description": "浮点类型",
        "constraints": ["float"],  # float 暂时用具体类型约束
        "pass_by": "value",
    },
    # 比较和容器协议约束
    "T_comparable": {
        "description": "可比较类型（支持 <, >, == 等运算符）",
        "bound": "Comparable",
        "pass_by": "const_ref",
    },
    "T_sized": {
        "description": "有大小的类型（实现 __len__ 方法）",
        "bound": "Sized",
        "pass_by": "const_ref",
    },
    # 特殊类型（具体类型约束）
    "T_tuple": {
        "description": "tuple 类型（编译时大小）",
        "constraints": ["tuple"],
        "pass_by": "const_ref",
    },
}
CONFIG = {
    # 数学函数
    "abs": {
        "signatures": ["abs(x: T_numeric) -> T_numeric"],
        "templates": ["std::abs({x})"],
        "includes": ["<cmath>"],
        "priority": 1,
    },
    "pow": {
        "signatures": [
            "pow(x: T_int, y: T_int) -> T_int",
            "pow(x: T_float, y: T_float) -> T_float",
        ],
        "templates": [
            "static_cast<int>(std::pow({x}, {y}))",
            "std::pow({x}, {y})",
        ],
        "includes": ["<cmath>"],
        "priority": 1,
    },
    "sum": {
        "signatures": [
            "sum(iterable: Iterable[T_numeric], start: T_numeric = 0) -> T_numeric",
        ],
        "templates": [
            "mcpy::reduce_sum({iterable}{?start})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    # 容器函数
    "len": {
        "signatures": [
            "len(obj: T_sized) -> int",
        ],
        "templates": [],
        "includes": [],
        "priority": 1,
        "callback": "mcpy.builtin.len_callback:LenFunctionCallback",
    },
    "repr": {
        "signatures": [
            "repr(obj: list[T]) -> str",
            "repr(obj: dict[K, V]) -> str",
        ],
        "templates": [
            "mcpy::list_repr({obj})",
            "mcpy::dict_repr({obj})",
        ],
        "includes": ["mcpy/builtins.h", "mcpy/builtins/dict.h"],
        "priority": 1,
    },
    # 序列函数
    "enumerate": {
        "signatures": [
            "enumerate(iterable: dict_items[K, V], start: int = 0) -> enumerate_view[tuple[K, V]]",
            "enumerate(iterable: Iterable[T], start: int = 0) -> enumerate_view[T]",
        ],
        "templates": [
            "mcpy::enumerate({iterable}{?start})",
            "mcpy::enumerate({iterable}{?start})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "sorted": {
        "signatures": [
            "sorted(iterable: Iterable[T_comparable], reverse: bool = False) -> list[T_comparable]",
        ],
        "templates": [
            "mcpy::iterable_sorted<{T_comparable}>({iterable}{?reverse})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "reversed": {
        "signatures": [
            "reversed(sequence: Reversible[T]) -> list_reversed_view[T]",
            "reversed(mapping: dict[K, V]) -> dict_reversed_keys_view[K]",
            "reversed(mapping: dict) -> dict_reversed_keys_view[mc::variant]",
        ],
        "templates": [
            "mcpy::iterable_reversed({sequence})",
            "mcpy::dict_reversed<{K}>({mapping})",
            "mcpy::dict_reversed({mapping})",
        ],
        "includes": ["mcpy/builtins/global_functions.h", "mcpy/builtins/dict.h"],
        "priority": 1,
    },
    "min": {
        "signatures": [
            "min(*args: T_comparable) -> T_comparable",
            "min(iterable: Iterable[T_comparable]) -> T_comparable",
        ],
        "templates": [
            "mcpy::min({args})",
            "mcpy::iterable_min<{T_comparable}>({iterable})",
        ],
        "includes": ["mcpy/builtins.h", "mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "max": {
        "signatures": [
            "max(*args: T_comparable) -> T_comparable",
            "max(iterable: Iterable[T_comparable]) -> T_comparable",
        ],
        "templates": [
            "mcpy::max({args})",
            "mcpy::iterable_max<{T_comparable}>({iterable})",
        ],
        "includes": ["mcpy/builtins.h", "mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "any": {
        "signatures": [
            "any(iterable: Iterable[T]) -> bool",
        ],
        "templates": [
            "mcpy::iterable_any({iterable})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "all": {
        "signatures": [
            "all(iterable: Iterable[T]) -> bool",
        ],
        "templates": [
            "mcpy::iterable_all({iterable})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "zip": {
        "signatures": [
            "zip(iter1: Iterable[T1], iter2: Iterable[T2]) -> zip_view[T1, T2]",
        ],
        "templates": [
            "mcpy::zip_view({iter1}, {iter2})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    # 其他函数
    "print": {
        "signatures": ["print(*args) -> None"],
        "templates": ["mcpy::print({args})"],
        "includes": ["mcpy/builtins.h"],
        "priority": 1,
    },
    "range": {
        "signatures": [
            "range(stop: int) -> list[int]",
            "range(start: int, stop: int) -> list[int]",
        ],
        "templates": ["mcpy::range({stop})", "mcpy::range({start}, {stop})"],
        "includes": ["mcpy/builtins.h"],
        "priority": 1,
    },
    # 迭代器函数
    "iter": {
        "signatures": [
            "iter(iterable: Iterable[T]) -> Iterator[T]",
        ],
        "templates": [
            "mcpy::iter_wrapper({iterable})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
    "next": {
        "signatures": [
            "next(iterator: Iterator[T]) -> T",
            "next(iterator: Iterator[T], default: Optional[T]) -> Optional[T]",
        ],
        "templates": [
            "{iterator}.__next__<{T}>()",
            "{iterator}.__next__<{T}>({default})",
        ],
        "includes": ["mcpy/builtins/global_functions.h"],
        "priority": 1,
    },
}
